(function () {

    //获取显示页面数据 服务器分页
    var serverData = {};
    var serverTable;
    serverData.key =''
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "post",
                "cache": false,
                "url":window.rootPath+ '/ui/device/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var id=data.id
                        var operate ='<input type="checkbox" class="checkchildDevice">';
                        return operate;
                    }
                },
                {
                    data: 'device.deviceModel',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 15) ? '<font title="'+data+'">'+data.substring(0,15)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'device.deviceType',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 4) ? '<font title="'+data+'">'+data.substring(0,4)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'device.mainMaterial',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 4) ? '<font title="'+data+'">'+data.substring(0,4)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'device.flow'
                },
                {
                    data: 'device.pressure'
                },
                {
                    data: 'device.machine',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 3) ? '<font title="'+data+'">'+data.substring(0,3)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'device.memo',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'attachmentNum',
                    render: function (data, type, full, meta) {
                    	var dOut;
                    	if(data==null){
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
                    	}else{
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
                    	}
                    	return dOut;
                    }
                    
                }
                
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        //上传附件
        jQuery('.upLoadBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能对一条记录上传文档',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要上传文档的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            	var deviceId=selects[0].device.id;
            	var fileType=selects[0].device.deviceType;
            	var url='/ui/device';
            	fileUpload(deviceId,fileType,serverTable);  	
            };
        });
       
        
        //下载附件
        jQuery('.downLoadBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能对一条记录下载文档',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要下载的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            	var deviceId=selects[0].device.id;
            	var fileType=selects[0].device.deviceType
            	filedown(deviceId,fileType,'1');
            };
        });
        
        

        //删除成套设备
        jQuery('.delBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
            var url=window.rootPath+"/ui/device/delDevice";
            delSelect(selects,url,serverTable)
        });

        //添加销售人员属性，弹窗清空
        jQuery('.addBtn').on('click', function () {
            jQuery('#id').val('');
            jQuery('#deviceModel').val('');
            jQuery('#deviceType').val('');
            jQuery('#mainMaterial').val('');
            jQuery('#flow').val('');
            jQuery('#pressure').val('');
            jQuery('#machine').val('');
            jQuery('#memo').val('');
            jQuery('#deviceModal').modal('show');
        });

        //编辑成套设备信息
        jQuery('.editBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能编辑一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要编辑的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
                jQuery('#id').val(selects[0].device.id);
                jQuery('#attachNumber').val(selects[0].attachNumber);
	            jQuery('#deviceModel').val(selects[0].device.deviceModel);
	            jQuery('#deviceType').val(selects[0].device.deviceType);
	            jQuery('#mainMaterial').val(selects[0].device.mainMaterial);
	            jQuery('#flow').val(selects[0].device.flow);
	            jQuery('#pressure').val(selects[0].device.pressure);
	            jQuery('#machine').val(selects[0].device.machine);
	            jQuery('#memo').val(selects[0].device.memo);
	            jQuery('#deviceModal').modal('show');
            };
        });
        //列表中的全选
        $("#checkAllDevice").click(function(){
            var check = $(this).prop("checked");
            $(".checkchildDevice").prop("checked", check);
        });
        
        //验证型号唯一性
        var validate=function(deviceType,id){
        	
        };
        //添加/编辑销售人员属性，保存
        var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#deviceEditForm") && !jQuery("#deviceEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var id=jQuery("#id").val();
            var deviceModel = jQuery('#deviceModel').val();
            var attachNumber = jQuery('#attachNumber').val();
            var deviceType = jQuery('#deviceType').val();
            var mainMaterial = jQuery('#mainMaterial').val();
            var flow = jQuery('#flow').val();
            var pressure = jQuery('#pressure').val();
            var machine = jQuery('#machine').val();
            var memo = jQuery('#memo').val();
            //验证型号唯一性
        		var d={
        				"id":id,
        				"deviceType":deviceType
        		}
        		var url=window.rootPath+'/ui/device/deviceValidate';
        		jQuery.ajax({
        			url:  encodeURI(url),
        			type: 'get',
        			data:d,
        			dataType: 'json',
        			success:function(data){
        				if(data.code>0){
        					message('设备型号重复');
        				}else{
        					//判断是编辑还是新增
        		            if(id == ""){ 
        		            	jQuery(".btn").prop('disabled',true);
        		                var api = window.rootPath + "/ui/device/add";
        		                var d = {
        		                    "deviceModel": deviceModel,
        		                    "deviceType": deviceType,
        		                    "mainMaterial": mainMaterial,
        		                    "flow": flow,
        		                    "pressure": pressure,
        		                    "machine": machine,
        		                    "memo": memo
        		                };
        		                jQuery.ajax({
        		                    type: 'post',
        		                    url: api,
        		                    data: JSON.stringify(d),
        		                    dataType: 'json',
        		                    contentType: 'application/json;charset=utf-8',
        		                    success: function (data) {
        		                        jQuery('#deviceModal').modal('hide');
        		                        jQuery(".btn").prop('disabled',false);
        		                        serverTable.draw(false);
        		                    }
        		                });
        		            		
        		            	
        		            }
        		            else{
        		                var data={
        		                    "id":id,
        		                    "attachNumber":attachNumber,
        		                    "deviceModel": deviceModel,
        		                    "deviceType": deviceType,
        		                    "mainMaterial": mainMaterial,
        		                    "flow": flow,
        		                    "pressure": pressure,
        		                    "machine": machine,
        		                    "memo": memo
        		                };
        		                var url=window.rootPath+'/ui/device/';
        		                jQuery.ajax({
        		                    url:encodeURI(url),
        		                    type:'patch',
        		                    data:JSON.stringify(data),
        		                    dataType:'json',
        		                    contentType:'application/json;charset=utf-8',
        		                    success: function () {
        		                        jQuery('#deviceModal').modal('hide');
        		                        serverTable.draw(false);
        		                    }

        		                });
        		            }
        				}
        			}
        		});
            
        });

        //查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //EXCEL导入
        jQuery('.btnImport').on("click",function()
        {
           fileUpload1();
        });
        /*
    	 * 导入文件上传插件,并刷新页面
    	 */
    	var fileUpload1= function() {
    		var root = window.rootPath;
    		WebFilesUploader(
    				{
    					_webuploader : {
    						server : root + '/ui/device/import',
    						chunked:false		
    					}
    				
    				}, function(jsons) {    				
    					 window.location.href=root+'/ui/device';
    				});
    	};
    	
    	///excel导出
    	jQuery('.btnExport').on("click",function() {
    		var selects=getSelect("#dataTable",1);
    		if(selects.length>0){
    			var key=selects[0].id;
    			for(var i=1;i<selects.length;i++){
    				   key += ',' + selects[i].id;  
    			 }
    			   var url=window.rootPath+ "/ui/device/exportSelects/"+key;
    			   window.location = url
    			
    		}else{
    			var key = jQuery('#keyword').val();
    			if (!key)
    				key = 'null';
    			var url = window.rootPath+ "/ui/device/export/" + key;
    			window.location = url;
    		}
			
		});

    });
})();


